<template>
  <div class="login-wrapper">
    <el-form
      class="login-form ele-bg-white"
      :rules="rules"
      :model="form"
    >
      <img src="@/assets/login_logo.png" style="width: 120px;height: auto;display: block; margin: 0 auto;margin-bottom: 10px">
      <el-form-item prop="mobile">
        <el-input
          placeholder="手机号"
          prefix-icon="el-icon-phone"
          v-model="form.mobile"
          oninput="value=value.replace(/[^\d]/g,'')"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="10">
          <el-col :sm="8">
            <el-input
              v-model="form.sms_code"
              type="phone"
              placeholder="验证码"
              prefix-icon="el-icon-info"
              oninput="value=value.replace(/[^\d]/g,'')"
            >
            </el-input>
          </el-col>
          <el-col :sm="16">
            <el-button
              class="full-btn"
              type="warning"
              @click="getSmsCode"
              :disabled="timeLoading"
            >{{ smsBtnText }}</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="密码"
          prefix-icon="el-icon-lock"
          v-model="form.password"
          type="password"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="确认密码"
          prefix-icon="el-icon-lock"
          v-model="form.verify_password"
          type="password"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="large"
          type="primary"
          class="full-btn"
          @click="formSubmit">
          重置密码
        </el-button>
      </el-form-item>
    </el-form>
    <div class="login-copyright">Copyright © 2018~{{(new Date()).getFullYear()}} 江苏嘉盛环境设备制造有限公司</div>
  </div>
</template>
<script>
export default {
  name: 'ForgetPassword',
  data() {
    return {
      direction: 0,
      form: {
        mobile: '',
        sms_code: '',
        password: '',
        verify_password: ''
      },
      timeLoading: false,
      smsBtnText: "获取验证码",
      smsBtnTime: 60
    }
  },
  computed: {
    rules() {
      return {
        mobile: [
          {required: true, message: this.$t('login.forget_password_form.mobile'), type: 'string', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    getSmsCode() {
      if (this.form.mobile.replace(/\s/gi, '') === '') {
        this.$message.error("请输入手机号码")
        return
      }
      this.actionSmsCode()
    },
    actionSmsCode() {
      this.$http.post('/verify_code_pwd', this.form).then((res) => {
        if (res.data.code === -1) {
          this.$message.error(res.data.msg)
          return false
        }
        this.timeLoading = true
        let that = this
        let smsInterval = setInterval(function() {
          if (that.smsBtnTime < 10) {
            that.smsBtnText = '重新获取 0' + that.smsBtnTime
          } else {
            that.smsBtnText = '重新获取' + that.smsBtnTime
          }
          // that.smsBtnText = that.smsBtnTime
          that.smsBtnTime--
          if (that.smsBtnTime < 0) {
            clearInterval(smsInterval)
            that.timeLoading = false
            that.smsBtnText = "获取验证码"
          }
        }, 1000)
        this.$message.success("验证码已发送至手机，请注意查收")
      })
    },
    formSubmit() {
      if (this.form.mobile.replace(/\s/gi, '') === '') {
        this.$message.error('请输入手机号码')
        return false
      }
      if (this.form.sms_code.replace(/\s/gi, '') === '') {
        this.$message.error('请输入验证码')
        return false
      }
      if (this.form.password.replace(/\s/gi, '') === '') {
        this.$message.error('请输入密码')
        return false
      }
      if (this.form.password.replace(/\s/gi, '') === '') {
        this.$message.error('请再次输入密码')
        return false
      }
      this.$http.post('/user/reset_password', this.form).then((res) => {
        if (!res.data.success) {
          this.$message.error(res.data.msg)
          return false
        }
        this.$message.success(res.data.msg)
        this.$router.push('/login')
      })
    }
  }
}
</script>
<style scoped>
.login-wrapper {
  padding: 50px 20px;
  position: relative;
  box-sizing: border-box;
  background-image: url("~@/assets/bg-login.png");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
}
.login-wrapper:before {
  content: "";
  background-color: rgba(0, 0, 0, .2);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.login-form {
  margin: 0 auto;
  width: 380px;
  max-width: 100%;
  padding: 25px 30px;
  position: relative;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
  border-radius: 4px;
  z-index: 2;
}
.full-btn {
  display: block;
  width: 100%;
}
.login-copyright {
  color: #eee;
  padding-top: 20px;
  text-align: center;
  position: relative;
  z-index: 1;
}
@media screen and (min-height: 550px) {
  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -220px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -220px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 20px;
    right: 0;
    left: 0;
  }
}

@media screen and (max-width: 768px) {
  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
